package com.sinya.shoppingmall;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author KoizumiSinya
 * @date 2016/1/13.
 */
public abstract class BaseFragmentActivity extends FragmentActivity {

    protected Context mContext;
    protected View rootView;
    protected Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;
        rootView = View.inflate(mContext, R.layout.activity_base_fragment, null);
        setContentView(rootView);

        initData();

        currentFragment = setCustomFragment();
        replaceFrameLayout(currentFragment);

        initTitle();
        initViews(rootView);
    }

    /**
     * 替换 R.id.fl_layout 位置显示的Fragment
     * @param fragment
     */
    private void replaceFrameLayout(Fragment fragment) {
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            //ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
            ft.replace(R.id.fl_layout, fragment);
            ft.commit();
        }
    }

    /**
     * 设置Title类型和点击响应
     */
    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText(setTitle());

        ImageView back = (ImageView) findViewById(R.id.img_back);
        back.setImageResource(R.drawable.btn_title_back_selector);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 由子类根据需要继承
     * 用来设置刚刚进入Activity的时候 需要获取传递的数据
     * 比如 Intent\Bundler 数据
     */
    protected void initData() {

    }

    /**
     * 由子类根据需要继承
     * 用于实例化布局控件
     *
     * @param rootView
     */
    protected void initViews(View rootView) {

    }

    /**
     * 子类必须继承
     * 用来设置Fragment
     * @return
     */
    protected abstract Fragment setCustomFragment();

    /**
     * 子类必须继承
     * 用来设置当前界面的Title文本
     * @return
     */
    protected abstract String setTitle();

}
